import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:gt_flutter_admin/services/api_service.dart';
import 'package:gt_flutter_admin/pages/system/role/role_view.dart';

import '../../../utils/toast_utils.dart';

class RoleController extends GetxController with StateMixin<RoleDataTable> {
  // 文本控件
  TextEditingController roleNameText = TextEditingController();

  @override
  void onReady() {
    super.onReady();

    onListRole();
  }

  /// 获取角色列表
  void onListRole() {
    change(null, status: RxStatus.loading());
    String? roleName = roleNameText.text.isEmpty ? null : roleNameText.text;
    listRole(roleName: roleName).then((page) {
      RoleDataTable sourceData = RoleDataTable(
        total: page.total.toInt(),
        rows: page.rows ?? [],
        controller: this,
      );
      change(sourceData, status: RxStatus.success());
    });
  }

  /// 删除角色
  void onDeleteRole(num roleId) {
    BrnDialogManager.showConfirmDialog(
      Get.context!,
      title: '是否删除该角色',
      cancel: '取消',
      confirm: '确定',
      onConfirm: () {
        deleteRole(roleId).then((value) {
          onListRole();
          ToastUtils.success('删除成功');
          Navigator.of(Get.context!).pop();
        });
      },
      onCancel: () => Navigator.of(Get.context!).pop(),
    );
  }
}
